﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace Light
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        Client client;
        Road road;

        int car_i = 0;
        void AddCar( Road road, int lane, int time )
        {            
            Car car = null;
            switch ( lane )
            {
                case 0:
                    car = new Car( canvas, road, pathNtoW.Data.GetFlattenedPathGeometry(), "car" + car_i.ToString(), lane, time );
                    break;
                case 1:
                    car = new Car( canvas, road, pathNtoS.Data.GetFlattenedPathGeometry(), "car" + car_i.ToString(), lane, time );
                    break;
                case 2:
                    car = new Car( canvas, road, pathNtoE.Data.GetFlattenedPathGeometry(), "car" + car_i.ToString(), lane, time );
                    break;
                case 3:
                    car = new Car( canvas, road, pathEtoN.Data.GetFlattenedPathGeometry(), "car" + car_i.ToString(), lane, time );
                    break;
                case 4:
                    car = new Car( canvas, road, pathEtoW.Data.GetFlattenedPathGeometry(), "car" + car_i.ToString(), lane, time );
                    break;
                case 5:
                    car = new Car( canvas, road, pathEtoS.Data.GetFlattenedPathGeometry(), "car" + car_i.ToString(), lane, time );
                    break;
                case 6:
                    car = new Car( canvas, road, pathStoE.Data.GetFlattenedPathGeometry(), "car" + car_i.ToString(), lane, time );
                    break;
                case 7:
                    car = new Car( canvas, road, pathStoN.Data.GetFlattenedPathGeometry(), "car" + car_i.ToString(), lane, time );
                    break;
                case 8:
                    car = new Car( canvas, road, pathStoW.Data.GetFlattenedPathGeometry(), "car" + car_i.ToString(), lane, time );
                    break;
                case 9:
                    car = new Car( canvas, road, pathWtoS.Data.GetFlattenedPathGeometry(), "car" + car_i.ToString(), lane, time );
                    break;
                case 10:
                    car = new Car( canvas, road, pathWtoE.Data.GetFlattenedPathGeometry(), "car" + car_i.ToString(), lane, time );
                    break;
                case 11:
                    car = new Car( canvas, road, pathWtoN.Data.GetFlattenedPathGeometry(), "car" + car_i.ToString(), lane, time );
                    break;
            }
            car_i++;
            road.AddCar( car );
        }

        public MainWindow()
        {
            InitializeComponent();

            // 创建公路
            road = new Road( canvas );

            // 在路口添加4栈灯，灯顺序如下
            //       |
            //       0
            // ---3--|--1---
            //       2
            //       |
            //       |

            TrafficLight light;
            // 交通灯0
            light = new TrafficLight( 0, canvas, road );
            light.X = 240;
            light.Y = 80;
            light.LoadImage( 180 );
            road.AddLight( light );

            // 交通灯1
            light = new TrafficLight( 1, canvas, road );
            light.X = 633;
            light.Y = 180;
            light.LoadImage( 270 );
            road.AddLight( light );

            //light.Status = LightStatus.YELLOW_FLASH;

            // 交通灯2
            light = new TrafficLight( 2, canvas, road );
            light.X = 520;
            light.Y = 450;
            light.LoadImage( 0 );
            road.AddLight( light );

           // light.Status = LightStatus.STRAIGHT_FLASH;

            // 交通灯3
            light = new TrafficLight( 3, canvas, road );
            light.X = 120;
            light.Y = 370;
            light.LoadImage( 90 );
            road.AddLight( light );

         //   light.Status = LightStatus.LEFT_FLASH;

            for ( int i = 0; i < 12; i++ )
            {
              //  AddCar( road, i, 10000 );
            }


            //road.AddCar( car7 );
            //Car car8 = new Car( canvas, road, pathStoN.Data.GetFlattenedPathGeometry(), "car8", 7, 5000 );
            //road.AddCar( car8 );
            //Car car9 = new Car( canvas, road, pathStoW.Data.GetFlattenedPathGeometry(), "car9", 8, 5000 );
            //road.AddCar( car9 );

            //Car car10 = new Car( canvas, road, pathWtoN.Data.GetFlattenedPathGeometry(), "car10", 9, 5000 );
            //road.AddCar( car10 );
            //Car car11 = new Car( canvas, road, pathWtoE.Data.GetFlattenedPathGeometry(), "car11", 10, 5000 );
            //road.AddCar( car11 );
            //Car car12 = new Car( canvas, road, pathWtoS.Data.GetFlattenedPathGeometry(), "car12", 11, 5000 );
            //road.AddCar( car12 );

            road.OnAnimate();

            // 创建客户端
            client = new Client( road );


            // 打开计时器
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler( dispatcherTimer_Tick );
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds( 50 );
            dispatcherTimer.Start();

            DispatcherTimer dispatcherTimer2 = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler( light_dispatcherTimer_Tick );
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds( 1000 );
            dispatcherTimer.Start();
        }

        private void dispatcherTimer_Tick( object sender, EventArgs e )
        {
            road.OnAnimate();
        }

        Random rand = new Random();

        private void light_dispatcherTimer_Tick( object sender, EventArgs e )
        {
            foreach ( TrafficLight light in road.LightList )
            {
                light.SetTime( light.remainedTime - 1 );
            }
        }

        private void canvas_MouseLeftButtonDown( object sender, MouseButtonEventArgs e )
        {
            ////创建移动动画
            //Point p = e.GetPosition( canvas );
            //Storyboard storyboard = new Storyboard();
            ////创建X轴方向动画
            //DoubleAnimation doubleAnimation = new DoubleAnimation(
            //  Canvas.GetLeft( rect ),
            //  p.X,
            //  new Duration( TimeSpan.FromMilliseconds( 500 ) )
            //);
            //Storyboard.SetTarget( doubleAnimation, rect );
            //Storyboard.SetTargetProperty( doubleAnimation, new PropertyPath( "(Canvas.Left)" ) );
            //storyboard.Children.Add( doubleAnimation );
            ////创建Y轴方向动画
            //doubleAnimation = new DoubleAnimation(
            //  Canvas.GetTop( rect ),
            //  p.Y,
            //  new Duration( TimeSpan.FromMilliseconds( 500 ) )
            //);
            //Storyboard.SetTarget( doubleAnimation, rect );
            //Storyboard.SetTargetProperty( doubleAnimation, new PropertyPath( "(Canvas.Top)" ) );
            //storyboard.Children.Add( doubleAnimation );
            ////将动画动态加载进资源内
            //if ( !Resources.Contains( "rectAnimation" ) )
            //{
            //    Resources.Add( "rectAnimation", storyboard );
            //}
            ////动画播放
            //storyboard.Begin();
        }

        private void button2_Click( object sender, RoutedEventArgs e )
        {
            foreach ( Car car in road.CarList )
            {
                car.CarResume();
            }
        }

        private void button1_Click( object sender, RoutedEventArgs e )
        {
            foreach( Car car in road.CarList)
            {
                car.CarPause();
            }
        }

        private void Image_ImageFailed( object sender, ExceptionRoutedEventArgs e )
        {

        }

        int ccc = 0;
        int right = 0;
        private void button1_Click_1( object sender, RoutedEventArgs e )
        {
            int c = rand.Next( 12 );
            if ( c % 3 == 0 )
            {
                AddCar( road, rand.Next( 12 ), 5000 );
            }
            else
                AddCar( road, c, 5000 );
        }



    }
}
